#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;

class BinaryIndexedTree {
private:
    vector<int> tree;

public:
    BinaryIndexedTree(int n) : tree(n + 1) {}

    void add(int i) {
        while (i < tree.size()) {
            tree[i] += 1;
            i += i & -i;
        }
    }

    int get(int i) {
        int res = 0;
        while (i > 0) {
            res += tree[i];
            i &= i - 1;
        }
        return res;
    }
};

void solve()
{
    ll n, m;
    cin >> n >> m;
    vector<int> a(n);
    for(int i = 0; i < n; i++) scanf("%d", &a[i]);
    vector<ll> pre(n + 1);
    for(int i = 0; i < n; i++) {
        pre[i + 1] = (pre[i] + a[i]) % m;
    }
    ll ans = 0;
    for(int i = 1; i <= n; i++) {
        ans += i * pre[i];
    }
    for(int i = 1; i <= n; i++) {
        ans -= pre[i - 1] * (n - i + 1);
    }
    BinaryIndexedTree t(m);
    ll cnt = 0;
    for(int i = 1; i <= n; i++) {
        cnt += i - 1 - t.get(pre[i]);
        t.add(pre[i]);
    }
    cout << ans + m * cnt << endl;
}


int main()
{
    int t = 1;
    // cin >> t;
    while(t--) solve();
    return 0;
}

